<template>
  <div class="h-full w-full flex flex-col justify-between">
    <div class="form px-[15px] pt-[15px] border-bottom">
      <el-form>
        <el-row :gutter="12">
          <el-col :span="3">
            <el-form-item>
              <el-input v-model="form.name" placeholder="用户名/昵称/备注" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-select class="!w-full" v-model="form.lottery_id">
                <el-option label="全部游戏" value="-1" />
                <el-option
                  v-for="item in gameTotal"
                  :key="item.LotteryId"
                  :label="item.Name"
                  :value="item.LotteryId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-select class="!w-full" v-model="form.action">
                <el-option label="默认排序" value="-1" />
                <el-option label="输赢降序" value="win_amount__asc" />
                <el-option label="输赢升序" value="win_amount__desc" />
                <el-option label="流水降序" value="amount__asc" />
                <el-option label="流水升序" value="amount__desc" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <div class="w-full flex justify-between">
                <el-date-picker
                  v-model="form.date"
                  type="daterange"
                  unlink-panels
                  range-separator="－"
                  start-placeholder="选择开始时间"
                  end-placeholder="选择结束时间"
                  :disabled-date="disableFutureDates"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click.stop="search">搜索</el-button>
              <el-button type="danger" @click.stop="handleRefresh">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="grow-[1] h-0 mx-[15px] mt-[10px]">
      <el-table
        scrollbar-always-on
        ref="elTableRef"
        v-loading="state.loading"
        :data="list"
        style="width: 100%"
        height="100%"
        border
        stripe
        fit
      >
        <template #empty>
          <ui-empty />
        </template>
        <el-table-column align="center" prop="LotteryName" label="彩种" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.LotteryName || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Cat1" label="玩法" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.Cat1 || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Count" label="笔数">
          <template #default="{ row }: TableItem">
            {{ row.Count || "--" }}
          </template>
        </el-table-column>
        <el-table-column prop="Amount" align="center" label="下注金额">
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.Amount"></ui-amount>
          </template>
        </el-table-column>
        <el-table-column prop="WinAmount" align="center" label="盈亏">
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.WinAmount"></ui-amount>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex justify-between my-[10px] mx-[15px]">
      <div class="flex-1 w-0 mr-[20px] flex justify-start">
        <div>
          <el-alert
            title="注意：报表最多保留两月数据。"
            type="warning"
            show-icon
            :closable="false"
          />
        </div>
      </div>
      <ui-pagination
        :total="state.total"
        v-model:pageSize="state.pageSize"
        v-model:currentPage="state.page"
        @current-change="getData"
      ></ui-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useList } from "@/hooks/useList";
import { getDateArray, disableFutureDates } from "@/utils/common";
import { reqReportClassify } from "@/api/report";
import { useGameStore } from "@/store/modules/game";
import { storeToRefs } from "pinia";
import moment from "moment";

interface TableItem {
  row: ClassifyMo;
}
interface Params {
  Name?: string;
  LotteryId?: string;
  SortField?: string;
  SortBy?: string;
  StartDate?: string;
  EndDate?: string;
}
const gameStore = useGameStore();
const { gameTotal } = storeToRefs(gameStore);

const form = reactive({
  lottery_id: "-1",
  name: "",
  action: "-1",
  date: getDateArray()["today"]
});

const getParams = () => {
  const params: Params = {
    StartDate: moment(form.date[0]).format("YYYY-MM-DD 00:00:00"),
    EndDate: moment(form.date[1]).format("YYYY-MM-DD 23:59:59")
  };
  if (form.name) {
    params.Name = form.name;
  }
  if (form.action && form.action != "-1") {
    const [field, sort] = form.action.split("__");
    params.SortField = field;
    params.SortBy = sort;
  }
  if (form.lottery_id && form.lottery_id != "-1") {
    params.LotteryId = form.lottery_id;
  }
  return params;
};

const { elTableRef, state, list, getData, search } = useList<ClassifyMo, Params>(
  reqReportClassify,
  getParams
);

// 重置
const handleRefresh = () => {
  form.lottery_id = "-1";
  form.name = "";
  form.action = "-1";
  form.date = getDateArray()["today"];
  search();
};

onMounted(() => {
  getData();
});
</script>
<style lang="scss" scoped></style>
